package com.quiz.action.sdk;

import com.quiz.action.sdk.ad.QActionVideoRewardCallback;
import com.quiz.action.sdk.ad.QActionlMaxReward;
import com.quiz.action.sdk.bridge.QActionCocosBridge;
import com.quiz.action.sdk.utils.QActionToastUtil;

import org.json.JSONObject;

public class QActionSDK {

    public static void showAd(String adSource){

        boolean isMaxReady =  QActionlMaxReward.getInstance().isReady();
        if (!isMaxReady){
            callbackAdError();
            return;
        }

        try {
            QActionlMaxReward.getInstance().showAd(QActionCocosBridge.mainGameActivity.get(), new QActionVideoRewardCallback() {
                @Override
                public void onAdOver() {
                    try {
                        JSONObject jsonObject =getJson(adSource);
                        QActionCocosBridge.getInstance().XSSdkCallback("ad_over_quiz", jsonObject.toString());
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }

                @Override
                public void onAdError() {
                    callbackAdError();
                }
            });
        } catch (Exception e) {
            callbackAdError();
        }
    }

    private static JSONObject getJson(String adSource) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("ad_source",adSource);
        }catch (Exception e){
            e.printStackTrace();

        }
        return jsonObject;
    }

    private static void callbackAdError() {
        try {
            QActionCocosBridge.getInstance().XSSdkCallback("ad_error_quiz", "{}");
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public static void showToast(final String message) {
        QActionToastUtil.showToast(message);
    }
}

